//
//  HomeFakeHeaderView.m
//  FD
//
//  Created by Yasin on 2018/10/12.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  h:上：300 下:kWhirligigHeight
/**
 2019-1-21 WaiSongYuYueView 5s显示有点变形
 上: 300 固定不变
 
 下: kWhirligigHeight 登录缩放,变化
 */

#import "HomeFakeHeaderView.h"
//自定义假的表头 collectionView的表头，让sectionHeaderView 作为表头。
//#import "WaiMaiDianCanAndYuYueView.h" //xib  12-8弃用
#import "WaiSongYuYueView.h" //12-8
#import "HomeHeaderUPView.h"
#import <SDCycleScrollView.h>

//model
#import "HomeWhirligigModel.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
@interface HomeFakeHeaderView() <SDCycleScrollViewDelegate>

/**上面的图片 xib*/
@property(nonatomic, strong) HomeHeaderUPView *upView;
/**中间的图片 xib*/
@property (nonatomic, strong) WaiSongYuYueView *wmdcAndYDView; //7.外卖订餐和预定view、 xib

/**第三部分 轮播图*/
@property (nonatomic, strong) UIView *lunbotuView;
/***/
@property (nonatomic,  strong) SDCycleScrollView *sdCycleView;


@end

@implementation HomeFakeHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor redColor];

        [self setupView];
    }
    return self;
}

//总高：上:300 + 下:kWhirligigHeight根据屏幕等比缩放
- (void)setupView {
    
    //加载xib view

    //上面的 h ：300
    self.upView = [[[NSBundle mainBundle] loadNibNamed:@"HomeHeaderUPView" owner:nil options:nil] firstObject];
    self.upView.frame = CGRectMake(0, 0, kScreenW, 300);
    [self addSubview:self.upView];
    
//    self.upView.backgroundColor = [UIColor blueColor];
    
    //参考原型进行布局。外卖点选预送这个view的背景是透明的 ，下面有一份是盖在上面的黑色背景上的。
    //外卖点餐view h：100
//    self.wmdcAndYDView = [[[NSBundle mainBundle] loadNibNamed:@"WaiMaiDianCanAndYuYueView" owner:nil options:nil] firstObject];
//    self.wmdcAndYDView.frame = CGRectMake(0, 170, kScreenW, 100);
    [self addSubview:self.wmdcAndYDView];
    
   
    //高 (kScreenW *0.5)+ 10(间距)
    //12-30轮播图比例3:2
    CGFloat lbt_Y = CGRectGetMaxY(self.upView.frame);
    
    self.lunbotuView.frame = CGRectMake(0, lbt_Y, kScreenW, kWhirligigHeight);
    [self addSubview:self.lunbotuView];
    
    
//    CGFloat sdH = (kScreenW-20) * 2 / 3;
    SDCycleScrollView *sdCycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(10, 0, kScreenW-20, (kWhirligigHeight)) delegate:self placeholderImage:nil];
    self.sdCycleView = sdCycleView;
    sdCycleView.backgroundColor = [UIColor whiteColor];//12-30
    [self.lunbotuView addSubview:sdCycleView];
    
    
    [self callBackAction];
}



- (void)callBackAction {
    
    FDWeakSelf;
    
    self.upView.homeHeaderUpViewClickLocationBlock = ^{
        if ([weakSelf.delegate respondsToSelector:@selector(homeFakeHeaderViewclickLocationBtnAction)]) {
            [weakSelf.delegate homeFakeHeaderViewclickLocationBtnAction];
        }
    };
    
    self.upView.homeHeaderUpViewClickYuYuBlock = ^{
        if ([weakSelf.delegate respondsToSelector:@selector(homeFakeHeaderViewclickMsgBtnAction)]) {
            [weakSelf.delegate homeFakeHeaderViewclickMsgBtnAction];
        }
    };
    
    //点击头像回调
    self.upView.homeHeaderUpViewClickAvatorBlock = ^{
        if ([weakSelf.delegate respondsToSelector:@selector(homeFakeHeaderViewClickAvatorAction)]) {
            [weakSelf.delegate homeFakeHeaderViewClickAvatorAction];
        }
    };
    
    //block回调
    self.wmdcAndYDView.clickDinWeiBlock = ^{
        if ([weakSelf.delegate respondsToSelector:@selector(homeFakeHeaderViewClickWaiMaiDianCanAction)]) {
            [weakSelf.delegate homeFakeHeaderViewClickWaiMaiDianCanAction];
        }
    };
    
    self.wmdcAndYDView.clickYuYueBlock = ^{
        if ([weakSelf.delegate respondsToSelector:@selector(homeFakeHeaderViewClickYuYueAction)]) {
            [weakSelf.delegate homeFakeHeaderViewClickYuYueAction];
        }
    };
}

- (void)setImgArr:(NSArray *)imgArr {
    _imgArr = imgArr;
    NSMutableArray *picURLArr = [NSMutableArray array];
    for (HomeWhirligigModel *model in imgArr) {
        [picURLArr addObject:model.thumbnail];
    }

    //给轮播图赋值
    self.sdCycleView.imageURLStringsGroup = [picURLArr copy];
}

#pragma mark - getter
- (UIView *)lunbotuView {
    if (!_lunbotuView) {
        _lunbotuView = [[UIView alloc] init];
//        _lunbotuView.backgroundColor = [UIColor yellowColor];
    }
    return _lunbotuView;
}
//12-8外卖点餐预约取餐view
- (WaiSongYuYueView *)wmdcAndYDView {
    if (!_wmdcAndYDView) {
        CGRect wmdcyyFrame = CGRectMake(0, 170, SCREEN_WIDTH, 120);
        _wmdcAndYDView = [[WaiSongYuYueView alloc] initWithFrame:wmdcyyFrame];
//        _wmdcAndYDView.backgroundColor = [UIColor yellowColor];
    }
    return _wmdcAndYDView;
}

#pragma mark -
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    FDLog(@"首页轮播图 点击了 第 %ld 张图",index);
    HomeWhirligigModel *whirligigModel = self.imgArr[index];
    FDLog(@"%@",whirligigModel);

    if ([NSString isEmptyString:whirligigModel.resource]) {
        FDLog(@"2019-2-16 1、首页轮播图补充逻辑：  判断结果字段：whirligigList.resource为空字符串式(无资源内容)时，改为不打开webview界面，直接点击无反应 商品列表页的轮播图同样处理");
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(homeFakeHeaderViewClickWhirligig:)]) {
        [self.delegate homeFakeHeaderViewClickWhirligig:whirligigModel.resource];
    }
}


#pragma mark - 2 layout

#pragma mark - 3 事件监听
//点击定位
- (void)locationBtnAction:(UIButton *)btn {
//    NSLog(@"定位");
    if ([self.delegate respondsToSelector:@selector(homeFakeHeaderViewclickLocationBtnAction)]) {
        [self.delegate homeFakeHeaderViewclickLocationBtnAction];
    }

}
//点击消息
- (void)clickMsgBtnAction:(UIButton *)btn {
    NSLog(@"点击消息");
    if ([self.delegate respondsToSelector:@selector(homeFakeHeaderViewclickMsgBtnAction)]) {
        [self.delegate homeFakeHeaderViewclickMsgBtnAction];
    }
}

//点击外面点餐
//点击预约



#pragma mark - 更新数据
- (void)updateLocationAddress:(NSString *)address {
    
    [self.upView.addressBtn setTitle:address forState:(UIControlStateNormal)];
    
    //左边文字右边图片(必须写,不然按钮更新文字后，会发生错乱的情况)
    [self.upView.addressBtn leftTextRightImageWithMargin:2.0];
}



/***请求数据并更新用户头像、昵称、积分、卡券数据*/
- (void)updateHomeUserNameJiFenKaQuanData {
    [self.upView requestHomeUserDataAndAssignData];
}


- (void)showRedDotView {
    [self.upView showRedDotView];
}
- (void)hideRedDotView {
    [self.upView hiddenRedDotView];
}

@end
